package cn.script.opt;

import cn.script.common.VsCode;
import cn.script.util.ParUtils;
import com.jacob.activeX.ActiveXComponent;

/**
 * 大漠插件
 */
public class DmSoft {

    private ActiveXComponent dm;

    public DmSoft() {
        this.dm = new ActiveXComponent("dm.dmsoft");
        int ret = this.dm.invoke("Reg", ParUtils.var("jv965720b239b8396b1b7df8b768c919e86e10f"),
                ParUtils.var("fwooad700")).getInt();
        if (ret != 1) {
            throw new RuntimeException("大漠插件注册失败");
        }
        this.dm.invoke("SetDict", ParUtils.var(0), ParUtils.var("C:\\mj\\mj.txt"));
        this.dm.invoke("UseDict", ParUtils.var(0));
    }


    public int bindWindow(long hwnd) {
        return this.dm.invoke("BindWindowEx",
                ParUtils.var(hwnd),
                ParUtils.var("gdi2"),
                ParUtils.var("dx.mouse.position.lock.api|dx.mouse.position.lock.message|dx.mouse.clip.lock.api|dx.mouse.input.lock.api|dx.mouse.state.api|dx.mouse.api|dx.mouse.cursor"),
                ParUtils.var("dx.keypad.input.lock.api|dx.keypad.state.api|dx.keypad.api"),
                ParUtils.var(""), ParUtils.var(0)).getInt();
    }

    public int bindWindowNormal(long hwnd) {
        return this.dm.invoke("BindWindowEx",
                ParUtils.var(hwnd),
                ParUtils.var("gdi2"),
                ParUtils.var("normal"),
                ParUtils.var("normal"),
                ParUtils.var(""), ParUtils.var(0)).getInt();
    }

    public int unWindow() {
        return this.dm.invoke("UnBindWindow").getInt();
    }


    public int waitKey(String code) {
        return this.dm.invoke("WaitKey", ParUtils.vars(VsCode.code(code), 0)).getInt();
    }


    public long getMousePointWindow() {
        return this.dm.invoke("getMousePointWindow").getInt();
    }

    /**
     * 字体识别
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @param color
     * @param sim
     * @return
     */
    public String ocr(int x1, int y1, int x2, int y2, String color, double sim) {
        return this.dm.invoke("Ocr", ParUtils.vars(x1, y1, x2, y2, color, sim)).getString();
    }

    /**
     * 鼠标移动的位置
     *
     * @param x1
     * @param y1
     * @return
     */
    public int moveTo(int x1, int y1) {
        return this.dm.invoke("MoveTo", ParUtils.vars(x1, y1)).getInt();
    }

    /**
     * 点击矩形内的任意一点
     *
     * @param x1
     * @param y1
     * @param w
     * @param h
     * @return
     */
    public void moveToEx(int x1, int y1, int w, int h) {
        this.dm.invoke("MoveToEx", ParUtils.vars(x1, y1, w, h));
    }

    /**
     * 左键点击
     *
     * @return
     */
    public int leftClick() {
        return this.dm.invoke("LeftClick").getInt();
    }

    /**
     * 右键点击
     *
     * @return
     */
    public int rightClick() {
        return this.dm.invoke("RightClick").getInt();
    }

    /**
     * 相对现在鼠标移动
     *
     * @return
     */
    public int moveR() {
        return this.dm.invoke("MoveR").getInt();
    }

    /**
     * 查找指定字体坐标
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @param text
     * @param color
     * @param sim
     * @return
     */
    public String findStrEx(int x1, int y1, int x2, int y2, String text, String color, double sim) {
        return this.dm.invoke("FindStrEx", ParUtils.vars(x1, y1, x2, y2, text, color, sim)).getString();
    }


    /**
     * 按键
     *
     * @param key
     * @return
     */
    public int keyPress(String key) {
        return this.dm.invoke("KeyPress", ParUtils.var(VsCode.code(key))).getInt();
    }
}
